var Relation = require('../relation');
var User = require('../user')
var Card = require('../card')
var Activity = require('../activity');
var initRedisCache = require('../../lib/InitRedisCache');
var Faction = require('../faction');
var fmproxy = require('../factionmember');
var Creditspolicy = require('../creditspolicy');
var FactionMember = require('../../entity').FactionMember;
var activity = require('../../entity').Activity;
var UserE = require('../../entity').User;
var CardE = require('../../entity').Card;
var redisManager = require('../redisManager');
var EventProxy = require('eventproxy');



function check(){
    // 某人在某圈子的 个数
    /*FactionMember.findOne({faction:'54102f517c99781e517c7d96', 'member.id':'5437b1e26f03d9591e7246a8'})
        .exec(function(err, fm){
            console.log(fm)
        })*/
    // 某人有多少个圈子
    /*FactionMember.find({'member.id':'53fdde3ced1f60f87d40dbb7'}).exec(function(err, fms){
        console.log(fms)
    })*/
    /*redisManager.isInFaction('', '53fdccc2f371cd1b755c0d8c', function(err, resp){
        console.log(resp)
    })*/

    //_checkAndUpdateUserFactions('53fdcd45f371cd1b755c0d90');
}

function _checkAllUserFactions(){
    User.getAllUsers(function(err, users){
        for(var i=0; i<users.length; i++){
            console.log(users[i]._id)
             _checkAndUpdateUserFactions(users[i]._id, function(){

             });
        }
    })
}

function _checkAndUpdateUserFactions(userId, cb){
    var ep = new EventProxy();
    //var userId = '53fdccc2f371cd1b755c0d8c'
    FactionMember.find({'member.id':userId}).exec(function(err, fms){
        var map = {};
        var fidarr = [];
        for(var i=0; i<fms.length; i++){
            var fm = fms[i];
            if(!map[fm.faction]){
                map[fm.faction] = fm;
                fidarr.push(fm.faction);
            }
            (function(fid){
                redisManager.isInFaction(fid, userId, function(err, resp){
                    console.log('User ['+userId+']是圈子:['+fid+'] 的人:'+resp);
                    if(!resp){
                        redisManager.addFactionMember(fid, userId, function(err, resp){
                            console.log('补充加入圈子:['+fid+']:'+resp);
                        })
                    }
                })
            })(fm.faction)
        }
        ep.after('factions', fidarr.length, function(list){
            console.log(list);
            UserE.update({_id: userId}, {$set: {factions: list}}, {}, function(err, user){
                console.log(user);
                cb();
            });
        })
        for(var j=0; j<fidarr.length; j++){
            var fid = fidarr[j];
            Faction.findById(fid, function(err, faction){
                ep.emit('factions', {
                    id: faction._id,
                    name: faction.name
                })
            })
        }
    });
}

check();

/*53fdcd45f371cd1b755c0d90
 53fdde3ced1f60f87d40dbb7
 53fed91f2056bfa6667a6128
 53fed9262056bfa6667a612e
 53fed9412056bfa6667a6148
 53fed9522056bfa6667a615c
 53fed9812056bfa6667a6189

 53fed9882056bfa6667a618e
 53feda3f2056bfa6667a67c6
 540beaf4b59f2dde293166da
 540eeb5966e5329b3feed255
 54102a537c99781e517c7d6d
 54102aab7c99781e517c7d73
 54102ab37c99781e517c7d7b
 54102b777c99781e517c7d86
 54102b8e7c99781e517c7d92
 541030437c99781e517c7d99

 541030607c99781e517c7d9d
 541031b47c99781e517c7da1
 5410328b7c99781e517c7da5
 541036e97c99781e517c7da9
 541037427c99781e517c7dad
 5410385e7c99781e517c7db4
 541043807c99781e517c7dcb
 5410460f7c99781e517c7de7
 5410467d7c99781e517c7dfa
 5412dfa81734ffd63774b6d7
 5413d7661734ffd63774b6f9
 5413d94b1734ffd63774b70e
 5413da8a1734ffd63774b724
 5413da961734ffd63774b728
 5413ddfe1734ffd63774b74c
 5413e2601734ffd63774b767
 5413e38b1734ffd63774b788
 5413e3ec1734ffd63774b7a7
 5413e3ed1734ffd63774b7a9
 5413e8fc1734ffd63774b7b0
 5413f50f1734ffd63774b7fb
 5413f5441734ffd63774b80a
 5413f8071734ffd63774b81d
 5413f8f91734ffd63774b82b
 5413f9c01734ffd63774b82f
 5413fa231734ffd63774b844
 53fed93b2056bfa6667a6142
 5413fa521734ffd63774b85b
 5413fd001734ffd63774b874
 5413ffaf1734ffd63774b878
 541404831734ffd63774b890
 54142dc3a281418464ff3c21
 54143dcd0c56eff174b18a11
 541445960c56eff174b18a15
 541447130c56eff174b18a19
 541448610c56eff174b18a1d
 54144d2a0c56eff174b18a28
 5414bcac0c56eff174b18a2c
 541c17331c2786c90166e7f9
 541c17351c2786c90166e7fb
 541c174c1c2786c90166e817
 541c17601c2786c90166e81b
 541c17a61c2786c90166e82d
 541c17e71c2786c90166e853
 541c17ed1c2786c90166e855
 541c18001c2786c90166e870
 541c18511c2786c90166e8a2
 541c18791c2786c90166e8c0
 541c197c1c2786c90166e8c4
 541c1a131c2786c90166e8c8
 541c1a401c2786c90166e8e8
 541c1a9a1c2786c90166e90a
 541c1b5d1c2786c90166e950
 541c1b931c2786c90166e978
 541c1bd11c2786c90166e9a2
 541c1c1c1c2786c90166e9a6
 541c20471c2786c90166ea27
 541cd3351c2786c90166ea8d
 53fdccc2f371cd1b755c0d8c
 541c30f11c2786c90166ea59
 53fed9512056bfa6667a6156
 53fed9612056bfa6667a616a
 53fed9722056bfa6667a617f
 53fed98d2056bfa6667a6197
 53fed9992056bfa6667a61a9
 53fed9b52056bfa6667a6277
 53fed9e52056bfa6667a63cf
 53fed9f72056bfa6667a658d
 53feda422056bfa6667a67ca
 53fedaf12056bfa6667a69dc
 5402b2da2056bfa6667a6ab1
 5402be392056bfa6667a6acb
 5402d7592056bfa6667a6ae3
 5402e4f12056bfa6667a6af7
 5403b4982056bfa6667a6b11
 53fed9292056bfa6667a6130
 53fed93f2056bfa6667a6144
 53fed96c2056bfa6667a6170
 53fed9732056bfa6667a6181
 53fed9902056bfa6667a6199
 53fed99c2056bfa6667a61b3
 53fed9c12056bfa6667a6297
 53fed9e72056bfa6667a6431
 53fed9ff2056bfa6667a65d0
 53feda442056bfa6667a681c
 53fedc4d2056bfa6667a6a9b
 5402b41c2056bfa6667a6ab5
 5402be882056bfa6667a6acf
 5402dd8d2056bfa6667a6ae7
 5402f01c2056bfa6667a6afb
 5403f08f2056bfa6667a6b15
 53fed9362056bfa6667a6138
 53fed9532056bfa6667a615e
 53fed96d2056bfa6667a6172
 53fed9902056bfa6667a619b
 53fed9a02056bfa6667a61b7
 53fed9cb2056bfa6667a629b
 53fed9f12056bfa6667a64d3
 53feda1f2056bfa6667a66e0
 53feda542056bfa6667a6870
 53fedf662056bfa6667a6aa2
 5402b9a72056bfa6667a6ab9
 5402cc462056bfa6667a6ad3
 5402de682056bfa6667a6aeb
 54031dbe2056bfa6667a6b05
 540845242056bfa6667a6b19
 53fed9382056bfa6667a613c
 53fed9442056bfa6667a614a
 53fed9582056bfa6667a6164
 53fed96e2056bfa6667a6174
 53fed9952056bfa6667a61a3
 53fed9a42056bfa6667a61bb
 53fed9ce2056bfa6667a62dd
 53fed9f52056bfa6667a650f
 53feda2e2056bfa6667a672c
 53feda722056bfa6667a68c6
 540037442056bfa6667a6aa7
 5402b9d52056bfa6667a6abf
 5402d2a52056bfa6667a6adb
 5402df8f2056bfa6667a6aef
 54031de12056bfa6667a6b09
 53fed9202056bfa6667a612a
 53fed93b2056bfa6667a6140
 53fed9482056bfa6667a6150
 53fed9592056bfa6667a6168
 53fed96f2056bfa6667a6177
 53fed98b2056bfa6667a6192
 53fed9962056bfa6667a61a5
 53fed9b22056bfa6667a6259
 53fed9db2056bfa6667a6327
 53fed9f62056bfa6667a654d
 53feda982056bfa6667a6924
 5402b2c72056bfa6667a6aad
 5402ba1b2056bfa6667a6ac3
 5402d2fd2056bfa6667a6adf
 5402e1662056bfa6667a6af3
 54032bf22056bfa6667a6b0d*/